package com.dragon.flow.threadLocal;

import com.dragon.flow.vo.login.PlusLoginUserVo;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;

import java.util.List;

/**
 * @title: 登录用户的ThreadLocal
 * @author: bruce.liu
 * @since: 2024/9/15 10:10
 */
public class LoginUserContext {
    private static final ThreadLocal<PlusLoginUserVo> loginUserThreadLocal = new ThreadLocal<>();

    private LoginUserContext() {
    }

    public static PlusLoginUserVo getUser() {
        PlusLoginUserVo baseUser = loginUserThreadLocal.get();
        return baseUser;
    }


    public static void addLoginUser(String userCode, List<String> roleSns, String companyCode) {
        if (!(StringUtils.isBlank(userCode) && CollectionUtils.isEmpty(roleSns))) {
            PlusLoginUserVo baseUser = new PlusLoginUserVo(userCode, roleSns, companyCode);
            loginUserThreadLocal.set(baseUser);
        }
    }

    public static void remove() {
        loginUserThreadLocal.remove();
    }
}
